import { SelectionModel } from '@angular/cdk/collections';
import { FlatTreeControl } from '@angular/cdk/tree';
import {
  Component,
  ElementRef,
  forwardRef,
  Inject,
  Injector,
  Input,
  OnInit,
  Renderer2,
  ViewChild,
  ViewContainerRef,
} from '@angular/core';
import { OrganizationService, Pageable } from 'sparrow-permission-lib';
import { switchMap } from 'rxjs/operators';
import { zip } from 'rxjs';
import { FlatNode } from 'src/app/domain/flat-node';
import { DynamicDataSourceForNzTreeService } from 'src/app/service/dynamic-data-source-for-nz-tree.service';
import { NzTreeCommonService } from 'src/app/service/nz-tree-common.service';
import { NzTreeGetChildren } from 'src/app/service/nz-tree-get-children';
import { NzTreeGetOrgChildrenService } from 'src/app/service/nz-tree-get-org-children-service.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-organization-search',
  templateUrl: './organization-search.component.html',
  styleUrls: ['./organization-search.component.css'],
})
export class OrganizationSearchComponent implements OnInit {
  constructor(
    private orgService: OrganizationService,
    private message: NzMessageService
  ) {
  }

  dataSource: any;
  hasChild = (_: number, node: FlatNode) => node.expandable;
  treeService: NzTreeCommonService = new NzTreeCommonService();
  selectedParent: any[] = []

  ngOnInit(): void {
    let getChildrenService: NzTreeGetChildren =
    new NzTreeGetOrgChildrenService(this.orgService);
  getChildrenService
    .getChildren(
      { id: 'root', label: 'root', level: -1, expandable: true, type: 'ORG' }
    )
    .subscribe(
      (res) => {
        this.dataSource = new DynamicDataSourceForNzTreeService(
          this.treeService.treeControl,
          res,
          getChildrenService
        );
      },
      (error) => {
        this.message.error('初始化失败！' + error);
      }
    );
  }


  onClose(event: any) {
    this.selectedParent = this.selectedParent.filter(f=>f!=event);
  }

  onItemSelect(node: any) {
    if (this.selectedParent.findIndex((f) => f.id === node.id) < 0)
      this.selectedParent.push(node);
  }


}
